package com.cskt.petproject178.controller;

import com.cskt.petproject178.pojo.Authened;
import com.cskt.petproject178.pojo.entity.Order;
import com.cskt.petproject178.pojo.param.AddOrderParam;
import com.cskt.petproject178.pojo.vo.PageVO;
import com.cskt.petproject178.pojo.vo.ResultVO;
import com.cskt.petproject178.pojo.vo.UserInfoVO;
import com.cskt.petproject178.service.OrderService;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.annotation.Resource;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 订单控制器
 *
 * @author 27013
 **/

@RestController
@RequestMapping(value = "/oms/order")
public class OrderController {

    private static final Logger log = LogManager.getLogger(OrderController.class);
    @Resource
    private OrderService orderService;
    @Resource
    private ObjectMapper objectMapper;
    @Resource
    private StringRedisTemplate stringRedisTemplate;
    @PostMapping(value = "/add")
    @Authened
    public ResultVO<?> orderAdd(@RequestHeader String Token, @RequestBody AddOrderParam param){
        String loginUserStr = stringRedisTemplate.opsForValue().get("pet:token:" + Token);
        //反序列化用户信息
        UserInfoVO userInfoVO;
        try {
             userInfoVO = objectMapper.readValue(loginUserStr, UserInfoVO.class);
        } catch (JsonProcessingException e) {
            log.error("反序列化用户信息失败",e);
            return ResultVO.ERR();
        }
        String orderNo = orderService.addOrder(userInfoVO.getId(), param);
        if (!StringUtils.hasText(orderNo)) {
            log.warn("订单创建失败，订单号:{}",orderNo);
            return ResultVO.ERR();
        }
        Map<String,String> map = new HashMap<>();
        map.put("orderNo", orderNo);
        return ResultVO.OK(map);
    }

    /**
     * 商品预提交
     * @param map
     * @return
     */
    @PostMapping(value = "/preSubmit")
    public ResultVO<?> preSubmit(@RequestBody Map<String, List<Map<String,Object>>> map){
        List<Map<String, Object>> skuInfos = map.get("skuInfos");
        return ResultVO.OK(orderService.selectPreSubmitCondition(skuInfos));
    }

    @GetMapping(value = "/detail/{orderNo}")
    @Authened
    public ResultVO<?> orderItemDetail(@PathVariable String orderNo){
        Order order = orderService.selectOrderByOrderNo(orderNo);
        if (!ObjectUtils.isEmpty(order)) {
            return ResultVO.OK(order);
        }
        return ResultVO.ERR();
    }


    @PostMapping(value = "/searchAllOrder/{pageNo}/{pageSize}")
    public ResultVO<?> searchAllOrder(@RequestHeader String Token, @PathVariable Integer pageNo, @PathVariable Integer pageSize,@RequestParam(value ="orderNo",required = false ) String orderNo,@RequestParam(value = "payStatus",required = false) Integer payStatus){
        //获取用户id
        String userStr = stringRedisTemplate.opsForValue().get("pet:token:" + Token);
        try {
            UserInfoVO user = objectMapper.readValue(userStr, UserInfoVO.class);
           PageVO<Order> orderPage = orderService.selectOrderParam(user.getId(), orderNo, payStatus, (pageNo - 1) * pageSize, pageSize);
            return ResultVO.OK(orderPage);
        } catch (JsonProcessingException e) {
            log.error("反序列化用户信息失败",e);
            return ResultVO.ERR();
        }
    }
}
